package com.dss.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.dss.dao.ExpertClassDAO;
import com.dss.dao.ExpertDAO;
import com.dss.dao.ExpertDocDAO;
import com.dss.po.Expert;
import com.dss.po.ExpertClass;
import com.dss.po.ExpertDoc;
import com.infomanage.exception.DaoException;
import com.jiuqi.webtree.util.StringUtils;

public class ExpertConsultationService {

	public final static int SEARCH_TYPE_KEYWORD = 0;
	public final static int SEARCH_TYPE_EXPERTCLASS = 1;
	public final static int SEARCH_TYPE_EXPERT = 2;
	public final static int SEARCH_TYPE_ALL = 3;
	
	public static HashMap<ExpertClass, List<Expert>> loadAllExpertsWithClass(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		HashMap<ExpertClass, List<Expert>> hashMap = new LinkedHashMap<ExpertClass, List<Expert>>();
		try {
			request.setCharacterEncoding("utf-8");
			ExpertClassDAO expertClassDAO = new ExpertClassDAO();
			List<ExpertClass> expertClasses = expertClassDAO.findAll(ExpertClass.class);
			ExpertDAO expertDao = new ExpertDAO();
			if (expertClasses != null) {
				for (ExpertClass expertClass : expertClasses) {
					List<Expert> experts = expertDao.findExpertsByClassId(expertClass.getExpertClassId());
					hashMap.put(expertClass, experts);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hashMap;
	}

	public static List<ExpertDoc> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		List<ExpertDoc> resultList = null;
		try {
			request.setCharacterEncoding("UTF-8");
			String searchType = request.getParameter("searchType");
			int search_type = SEARCH_TYPE_ALL;
			if(StringUtils.isNotEmpty(searchType)){
				search_type = Integer.parseInt(searchType);
			}
			switch (search_type) {
			case SEARCH_TYPE_KEYWORD:
				String keyword = request.getParameter("keyword");
				resultList = searchByKeyword(keyword);
				break;
			case SEARCH_TYPE_EXPERTCLASS:
				int expertClassId = Integer.parseInt(request.getParameter("expertClassId"));
				resultList = searchByExpertClass(expertClassId);
				break;
			case SEARCH_TYPE_EXPERT:
				int expertId = Integer.parseInt(request.getParameter("expertId"));
				resultList = searchByExpert(expertId);
				break;
			case SEARCH_TYPE_ALL:
				resultList = searchAll();
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList == null ? new ArrayList<ExpertDoc>() : resultList;
	}
	
	private static List<ExpertDoc> searchByKeyword(String keyword){
		ExpertDocDAO dao = new ExpertDocDAO();
		try {
			return dao.findDocsByKeyword(keyword);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static List<ExpertDoc> searchByExpert(int expertId){
		ExpertDocDAO dao = new ExpertDocDAO();
		try {
			return dao.findDocsByExpertId(expertId);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static List<ExpertDoc> searchByExpertClass(int expertClassId) {
		List<ExpertDoc> expertDocList = new ArrayList<ExpertDoc>();
		ExpertDAO expertDao = new ExpertDAO();
		ExpertDocDAO expertDocDAO = new ExpertDocDAO();
		List<Expert> experts;
		try {
			experts = expertDao.findExpertsByClassId(expertClassId);
			if (experts != null) {
				for (Expert expert : experts) {
					List<ExpertDoc> list = expertDocDAO.findDocsByExpertId(expert.getExpertId());
					if (list != null) {
						expertDocList.addAll(list);
					}
				}
			}
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return expertDocList;
	}
	
	private static List<ExpertDoc> searchAll(){
		ExpertDocDAO dao = new ExpertDocDAO();
		try {
			return dao.findAll(ExpertDoc.class);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return null;
	}
}
